package com.jobsleaf.tableviewer;

import java.util.List;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.part.ViewPart;

public class AuditView extends ViewPart
{
	private TableViewer mViewer;

	public void createPartControl(Composite parent)
	{
		createTableViewer(parent);
	}

	public void setFocus()
	{
	}

	private void createTableViewer(Composite parent)
	{
		mViewer = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
		mViewer.setLabelProvider(new TableLabelProvider());
		mViewer.setContentProvider(new MyContentProvider());
		String[] columnHeading = new String[] { "Audit Name", "AuditID", "Schedule Id" };
		Table table = mViewer.getTable();
		for (int i = 0; i < columnHeading.length; i++)
		{

			TableColumn tab = new TableColumn(table, SWT.BOLD);
			tab.setText(columnHeading[i]);
			tab.setWidth(100);

		}
		List<Audit> list = TableUtility.getAuditList();
		mViewer.setInput(list.toArray());
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		final MenuManager mm = new MenuManager("view.popupmenu");
		final Menu menu = mm.createContextMenu(mViewer.getTable());
		mViewer.getTable().setMenu(menu);
		getSite().registerContextMenu(mm, mViewer);

		getSite().setSelectionProvider(mViewer);

	}

	public void refresh(List<Audit> list)
	{
		mViewer.getTable().clearAll();
		mViewer.setInput(list.toArray());
	}

}
